package com.example.word.fragments

import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.LifecycleOwner
import androidx.navigation.fragment.findNavController
import com.example.word.Injection
import com.example.word.data.room.Word
import com.example.word.databinding.FragmentAddBinding
import com.example.word.viewModels.WordViewModel
import kotlinx.android.synthetic.main.fragment_add.*

class AddFragment : Fragment(), LifecycleOwner {

    private lateinit var imm: InputMethodManager
    private val wordViewModel by viewModels<WordViewModel> {
        Injection.provideWordViewModelFactory(requireActivity())
    }
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d("TAG", "onCreateView: ")
        // Inflate the layout for this fragment
        val binding = FragmentAddBinding.inflate(inflater, container, false)
        binding.buttonAdd.isEnabled = false

        val textWatcher = object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                val english = editTextTextEnglish.text.toString().trim()
                val chineseMeaning = editTextTextChineseMeaning.text.toString().trim()
                buttonAdd.isEnabled = (english.isNotEmpty() && chineseMeaning.isNotEmpty())
            }


        }
        binding.editTextTextEnglish.addTextChangedListener(textWatcher)
        binding.editTextTextChineseMeaning.addTextChangedListener(textWatcher)

        binding.buttonAdd.setOnClickListener {
            val english = editTextTextEnglish.text.toString().trim()
            val chineseMeaning = editTextTextChineseMeaning.text.toString().trim()
            val word = Word(word = english, chineseMeaning = chineseMeaning)
            wordViewModel.insertWords(word)

            val controller = findNavController()
            controller.navigateUp()
        }
        binding.editTextTextChineseMeaning.setOnFocusChangeListener { view, b ->
            if (!b) {
                imm.hideSoftInputFromWindow(view.windowToken, 0)
            }
        }
        binding.editTextTextEnglish.setOnFocusChangeListener { view, b ->
            if (!b) {
                imm.hideSoftInputFromWindow(view.windowToken, 0)
            }
        }

        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        editTextTextEnglish.requestFocus()
        imm = requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(editTextTextEnglish, 0)
    }
}